package com.yuju.controller.login;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.redis.jfinal.plugin.Redis;
import com.yuju.common.Result;
import com.yuju.constant.CacheKey;
import com.yuju.constant.Config;
import com.yuju.constant.Constant;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.LoginReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.model.MemberModel;
import com.yuju.group.service.IMemberLevelService;
import com.yuju.group.service.IMemberService;
import com.yuju.login.model.OpModel;
import com.yuju.login.service.ILoginService;
import com.yuju.util.ConfigUtil;
import com.yuju.util.TokenUtils;
import com.yuju.util.Utils;
import com.yuju.vo.login.OpVO;

@Controller
@RequestMapping("wxLogin")
public class LoginController extends BaseController<OpVO, OpModel>{
	
	@Autowired
	private ILoginService loginService;
	
	@Autowired
	private IMemberService memberService;
	
	@Autowired
	private IMemberLevelService memberLevelService;
	
	/**
	 *
	 * 功能：密码登录<br/>
	 *
	 * @author zhousc
	 * @Date 2018年1月8日 上午9:46:58 <br/>
	 */
	@RequestMapping("pwdLogin")
	@ResponseBody
	public Result pwdLogin(@RequestBody JSONObject obj){
		OpVO opVO = obj.toJavaObject(OpVO.class);
		
		Result result = new Result();
		OpModel opModel = loginService.wxLogin(convert(opVO));
		
		// 将用户信息放入缓存
		cacheUser(opModel);
		if (Utils.isNotNull(opModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());

			// 查询会员信息
			setMemberInfo(opModel);

			//用于包装返回数据
			Map<String, Object> returnMap = new HashMap<>();
			
			// 子系统访问接口
			Map<String, String> urls = new HashMap<String, String>();
			urls.put(Constant.URL_PMS_SERVER, ConfigUtil.get(Constant.URL_PMS_SERVER));
			urls.put(Constant.URL_IMS_BACK_SERVER, ConfigUtil.get(Constant.URL_IMS_BACK_SERVER));
			urls.put(Constant.URL_GUEST_SERVER, ConfigUtil.get(Constant.URL_GUEST_SERVER));
			urls.put(Constant.URL_FILE_SERVER, ConfigUtil.get(Constant.URL_FILE_SERVER));
			urls.put(Constant.URL_IMG_SERVER, ConfigUtil.get(Constant.URL_IMG_SERVER));
			returnMap.put("urls", urls);
			
			returnMap.put("opInfo", opModel);
			
			result.setData(returnMap);
			
			result.setReturnCode(LoginReturnCodeEnum.LOGIN_SUCCESS.getCode());
			result.setReturnMsg(LoginReturnCodeEnum.LOGIN_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(LoginReturnCodeEnum.LOGIN_FAILURE.getCode());
			result.setReturnMsg(LoginReturnCodeEnum.LOGIN_FAILURE.getDesc());
		}
		return result;
	}
	
	
	/**
	 *
	 * 功能：验证码登录<br/>
	 *
	 * @author zhousc
	 * @Date 2018年1月8日 上午9:46:58 <br/>
	 */
	@RequestMapping("verificationCodeLogin")
	@ResponseBody
	public Result verificationCodeLogin(@RequestBody JSONObject obj){
		OpVO opVO = obj.toJavaObject(OpVO.class);
		
		Result result = new Result();
		OpModel opModel = loginService.verificationCodeLogin(convert(opVO));
		
		// 将用户信息放入缓存
		cacheUser(opModel);
		
		if (Utils.isNotNull(opModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			
			// 查询会员信息
			setMemberInfo(opModel);
			
			//用于包装返回数据
			Map<String, Object> returnMap = new HashMap<>();
			
			// 子系统访问接口
			Map<String, String> urls = new HashMap<String, String>();
			urls.put(Constant.URL_PMS_SERVER, ConfigUtil.get(Constant.URL_PMS_SERVER));
			urls.put(Constant.URL_IMS_BACK_SERVER, ConfigUtil.get(Constant.URL_IMS_BACK_SERVER));
			urls.put(Constant.URL_GUEST_SERVER, ConfigUtil.get(Constant.URL_GUEST_SERVER));
			urls.put(Constant.URL_FILE_SERVER, ConfigUtil.get(Constant.URL_FILE_SERVER));
			returnMap.put("urls", urls);
			
			returnMap.put("opInfo", opModel);

			result.setData(returnMap);
			result.setReturnCode(LoginReturnCodeEnum.LOGIN_SUCCESS.getCode());
			result.setReturnMsg(LoginReturnCodeEnum.LOGIN_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(LoginReturnCodeEnum.VAILD_FAILURE.getCode());
			result.setReturnMsg(LoginReturnCodeEnum.VAILD_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 *
	 * 功能：静默登录<br/>
	 *
	 * @author xuehj
	 * @Date 2018年1月23日 下午20:13:58 <br/>
	 */
	@RequestMapping("quietLogin")
	@ResponseBody
	public Result quietLogin(@RequestBody JSONObject obj) {
		String opId = obj.getString("id");
		Result result = new Result();
		
		if (Utils.isNull(opId)) {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(LoginReturnCodeEnum.NEED_USER_ID.getCode());
			result.setReturnMsg(LoginReturnCodeEnum.NEED_USER_ID.getDesc());
			return result;
		}
		
		OpModel opModel = loginService.quietLogin(opId);
		
		if (Utils.isNotNull(opModel)) {
			// 将用户信息放入缓存
			cacheUser(opModel);
			
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());

			// 查询会员信息
			setMemberInfo(opModel);

			//用于包装返回数据
			Map<String, Object> returnMap = new HashMap<>();
			
			// 子系统访问接口
			Map<String, String> urls = new HashMap<String, String>();
			urls.put(Constant.URL_PMS_SERVER, ConfigUtil.get(Constant.URL_PMS_SERVER));
			urls.put(Constant.URL_IMS_BACK_SERVER, ConfigUtil.get(Constant.URL_IMS_BACK_SERVER));
			urls.put(Constant.URL_GUEST_SERVER, ConfigUtil.get(Constant.URL_GUEST_SERVER));
			urls.put(Constant.URL_FILE_SERVER, ConfigUtil.get(Constant.URL_FILE_SERVER));
			urls.put(Constant.URL_IMG_SERVER, ConfigUtil.get(Constant.URL_IMG_SERVER));
			returnMap.put("urls", urls);
			
			returnMap.put("opInfo", opModel);
			
			result.setData(returnMap);
			
			result.setReturnCode(LoginReturnCodeEnum.LOGIN_SUCCESS.getCode());
			result.setReturnMsg(LoginReturnCodeEnum.LOGIN_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(LoginReturnCodeEnum.LOGIN_FAILURE.getCode());
			result.setReturnMsg(LoginReturnCodeEnum.LOGIN_FAILURE.getDesc());
		}
		return result;
	}
	
	
	/**
	 * 
	 * 功能：将用户信息放入缓存<br/>
	 *
	 * @author xuehj
	 * @Date 2017年12月13日 下午1:55:37 <br/>
	 */
	private void cacheUser(OpModel user) {
		String token = TokenUtils.create(user.getName());
		user.setToken(token);
		Redis.use().setex(token + CacheKey.KEY_CACHE_USER, Config.CACHE_DEFAULT_TIME_SECONDS, JSON.toJSONString(user));
	}
	
	private void setMemberInfo(OpModel user) {
		MemberModel paramModel = new MemberModel();
		paramModel.setId(user.getUserId());
		MemberModel member = memberService.getMember(paramModel);
		
		if (null != member) {
			user.setUserName(member.getName());
			user.setLevelId(member.getMemberLevelId());
			user.setLevelName(member.getMemberLevelName());
		}
	}
}
